﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Data;

namespace CNVP.JShop.Publish
{
    public class UnitLabel
    {
        /// <summary>
        /// 标签名称
        /// </summary>
        public string UnitName { get; set; }
        /// <summary>
        /// 是否发布(True发布，Flase预览)
        /// </summary>
        public bool TemplatePublish { get; set; }
        /// <summary>
        /// 栏目序号
        /// </summary>
        public int ColumnID { get; set; }
        /// <summary>
        /// 方案名称
        /// </summary>
        public string ProjectName { get; set; }
        public int StyleID { get; set;}
        /// <summary>
        /// 模板类型：0首页，1列表，2内页
        /// </summary>
        public int ModuleType { get; set; }
        private CNVP.JShop.BLL.CNVP_UnitsInfo bll = new CNVP.JShop.BLL.CNVP_UnitsInfo();
        /// <summary>
        /// 解析标签内容
        /// </summary>
        /// 
        public string ParseLabelConetent(bool canEdit)
        {
            DataTable Dt = bll.GetUnitStyle(UnitName, ProjectName);
            if (canEdit)
            {
                if (Dt != null && Dt.Rows.Count > 0)
                {
                    return UnitLabelHtml(Dt);
                }
                else
                {
                    return UnitLabelAdd();
                }
            }
            else
            {
                if (Dt != null && Dt.Rows.Count > 0)
                {
                    return UnitLabelHtmlShow(Dt);
                }
                else
                {
                    return String.Empty;
                }
            }
        }
        public string ParseLabelConetent()
        {
            DataTable Dt = bll.GetUnitStyle(UnitName, ProjectName);
            if (Dt != null && Dt.Rows.Count > 0)
            {
                return UnitLabelHtml(Dt);
            }
            else
            {
                return UnitLabelAdd();
            }
        }
        /// <summary>
        /// 获取单元标签的值
        /// </summary>
        /// <param name="Dt"></param>
        /// <returns></returns>
        private string UnitLabelHtml(DataTable Dt)
        {
            string Str = "";
            string _UnitName = Dt.Rows[0]["UnitName"].ToString();
            string _UnitID = Dt.Rows[0]["UnitID"].ToString();
            string _StyleID = Dt.Rows[0]["StyleID"].ToString();
            string _UnitHtml = Dt.Rows[0]["UnitHtml"].ToString();  
            if (TemplatePublish)
            {
                Str=_UnitHtml;
            }
            else
            {
                if (!string.IsNullOrEmpty(_UnitHtml))
                {
                    Str = string.Format("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"border:1px dotted red; cursor:hand;\" onclick =\"OpenUnitSettingWindow({2},'{1}',{3},{4},{3});\" onmouseover =\"init({0},'{1}',{2},{3},1);\" onmouseout=\"init('','','','',0);\">{5}</td></tr></table>", ColumnID, _UnitName, _UnitID, _StyleID, ModuleType, _UnitHtml);
                }
                else
                {
                    Str = string.Format("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"border:1px dotted red; cursor:hand;\" onclick =\"OpenUnitSettingWindow({2},'{1}',{3},{4},{3});\" onmouseover =\"init({0},'{1}',{2},{3},{4},1);\" onmouseout=\"init('','','','','',0);\">{1}</td></tr></table>", ColumnID, _UnitName, _UnitID, _StyleID, ModuleType);
                }
            }
            return Str;
        }
        private string UnitLabelHtmlShow(DataTable Dt)
        {
            string Str = "";
            string _UnitName = Dt.Rows[0]["UnitName"].ToString();
            string _UnitID = Dt.Rows[0]["UnitID"].ToString();
            string _StyleID = Dt.Rows[0]["StyleID"].ToString();
            string _UnitHtml = Dt.Rows[0]["UnitHtml"].ToString();
            if (TemplatePublish)
            {
                Str = _UnitHtml;
            }
            else
            {
                if (!string.IsNullOrEmpty(_UnitHtml))
                {
                    Str = _UnitHtml;
                }
                else
                {
                    Str =  _UnitName;
                }
            }
            return Str;
        }
        /// <summary>
        /// 添加单元标签
        /// </summary>
        /// <returns></returns>
        private string UnitLabelAdd()
        {
            string Str = "";
            if (!TemplatePublish)
            {
                int _UnitID = 0;
                CNVP.JShop.BLL.CNVP_UnitsInfo bll = new CNVP.JShop.BLL.CNVP_UnitsInfo();
                bll.AddUnitStyle(UnitName,ProjectName, out _UnitID);
                Str = string.Format("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"border:1px dotted red; cursor:hand;\" onclick =\"OpenUnitSettingWindow({1},'{3}',{2},{4},{2});\" onmouseover =\"init({0},{3},{1},{2},{4},1);\" onmouseout=\"init('','','','','',0);\">{3}</td></tr></table>", ColumnID, _UnitID, 0, UnitName, ModuleType);
            }
            return Str;
        }
    }
}